﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WSMP.UserControls
{
    public partial class Grid : System.Web.UI.UserControl
    {
        public string NewButtonTitle { get; set; }

        public delegate void RowDataBound(object sender, GridViewRowEventArgs e);
        public event RowDataBound GridRowDataBound;

        public delegate void RowCreate(object sender, EventArgs e);
        public event RowCreate GridRowCreate;

        public string NewButtonHref
        {
            set
            {
                lbtnAhref.HRef = value;
            }
        }
        public GridView GrdView
        {
            set { this.grdGridView = value; }
            get { return this.grdGridView; }
        }
        public DataTable DataSource
        {
            get
            {
                return (DataTable)this.grdGridView.DataSource ?? new DataTable();
            }
            set
            {
                this.grdGridView.DataSource = value;
                this.grdGridView.DataBind();
                ViewState["data"] = value;
            }
        }

        public DropDownList SearchBy
        {
            get { return this.ddlSearchBy; }
            set { this.ddlSearchBy = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void grdGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdGridView.DataSource = (DataTable)ViewState["data"];
            grdGridView.PageIndex = e.NewPageIndex;
            grdGridView.DataBind();
        }

        protected void btnSearchBy_Click(object sender, EventArgs e)
        {
            DataTable dt = ((DataTable)ViewState["data"]);
            var drArray = dt.Select(String.Format(ddlSearchBy.SelectedValue, " like '%", txtSearchBy.Text, "%'"));
            if (drArray != null)
            {
                grdGridView.DataSource = drArray.CopyToDataTable();
                grdGridView.DataBind();
            }

        }



        protected void OnRowDataBound(GridViewRowEventArgs e)
        {
            if (GridRowDataBound != null)
                GridRowDataBound(this, e);
        }

        protected void grdGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            OnRowDataBound(e);
        }

        protected void grdGridView_Init(object sender, EventArgs e)
        {
            OnRowInit(e);
        }

        protected void OnRowInit(EventArgs e)
        {
            if (GridRowCreate != null)
                GridRowCreate(this, e);
        }



    }
}